package com.henry.chatkit.input

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.henry.chatkit.R


class MorePagerAdapter(val data: List<InputMoreActionUnit>, var context: Context) : RecyclerView.Adapter<MorePagerAdapter.ViewPagerViewHolder>() {

    private val ITEM_COUNT_PER_GRID_VIEW = 8

    private var mItemCount = (data.size + ITEM_COUNT_PER_GRID_VIEW - 1) / ITEM_COUNT_PER_GRID_VIEW

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewPagerViewHolder {
        return ViewPagerViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.chat_input_more_page, parent, false))
    }

    override fun onBindViewHolder(holder: ViewPagerViewHolder, position: Int) {

        val end = if ((position + 1) * ITEM_COUNT_PER_GRID_VIEW > data.size) data.size
        else (position + 1) * ITEM_COUNT_PER_GRID_VIEW

        val subBaseActions = data.subList(position * ITEM_COUNT_PER_GRID_VIEW, end)

        val layoutManager = GridLayoutManager(context, 4)
        val adapter = MoreItemAdapter(subBaseActions)

        holder.recyclerView.layoutManager = layoutManager
        holder.recyclerView.adapter = adapter

    }

    override fun getItemCount(): Int {
        return mItemCount
    }


    class ViewPagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val recyclerView: RecyclerView by lazy { itemView.findViewById(R.id.moreRecycleView) }
    }
}